package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.dto.AssetsInfoDTO;
import com.key.entity.Assets;
import com.key.entity.MainInfoSingle;
import com.key.mapper.MainInfoMapper;
import com.key.service.MainInfoService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.OddNumbersUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: li
 * @CreateTime: 2023-02-24  14:44
 */
@Service
public class MainInfoServiceImpl implements MainInfoService {

    @Autowired
    MainInfoMapper mainInfoMapper;

    @Override
    @Transactional
    public R addMainInfo(Assets assets, Integer[] ids) {

        R r = new R();
     //   String[] split = ids.split(",");

        //添加维保信息单信息
        MainInfoSingle single = new MainInfoSingle();
        //得到后台管理员名字，这儿暂时为张三
        single.setProcessingPerson("张三");
        single.setMainInfoNum(OddNumbersUtils.creatON("WB"));
        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        single.setWorkDate(date);
        single.setSupplier(assets.getSupplier());
        single.setContacts(assets.getContacts());
        single.setContactInfo(assets.getContactInfo());
        single.setPersonInCharge(assets.getPersonInCharge());
        single.setExpirationTime(assets.getExpirationTime().toString());
        single.setExplain(assets.getExplain());

        try {
            int i = mainInfoMapper.insertMainInfo(single);

            for (Integer id:ids){
                assets.setAssetsId(id.longValue());
                //修改资产的维保信息
                int i1 = mainInfoMapper.updateAssetsMainInfo(assets);
                //添加资产和维保信息单的关系表
                HashMap map = new HashMap();
                map.put("mainInfoNum",single.getMainInfoNum());
                map.put("assetsId",id);
                int i2 = mainInfoMapper.insertMisAssets(map);
            }

            if (i>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
            else {
                r.setCode(500);
                r.setMsg("添加失败");
            }
        } catch (NumberFormatException e) {
            throw  new TranException("事务异常");
        }

        return r;
    }

    @Override
    public R showAllMainInfoSingle(MainInfoSingle mainInfoSingle) {
        R r = new R();
        try {
            List<MainInfoSingle> mainInfoSingles = mainInfoMapper.selectAllMainInfo(mainInfoSingle);
            r.setCode(200);
            r.setMsg("success");
            r.setData(mainInfoSingles);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public R showMainInfoById(Integer id) {
        R r = new R();
        try {
            MainInfoSingle mainInfoSingle = mainInfoMapper.selectMainInfoById(id);
            r.setCode(200);
            r.setMsg("success");
            r.setData(mainInfoSingle);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public R showAssetsByMainNum(String mainInfoNum) {
        R r = new R();
        try {
            List<AssetsInfoDTO> dtoList = mainInfoMapper.selectAssetsByMainNum(mainInfoNum);
            r.setCode(200);
            r.setMsg("success");
            r.setData(dtoList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public R deleteMainInfoById(Integer[] ids) {


        R r = new R();


        try {
            int i=0;
            for (Integer id:ids){
                i = mainInfoMapper.deleteMainInfoById(id);
                i++;
            }
            if (i>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
            else {
                r.setCode(500);
                r.setMsg("删除失败");
            }
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public HSSFWorkbook getWork() {

//        String[] header = {"主键ID","变更单号","变更状态","业务日期","处理人","业务所属公司","资产id列表","资产类别","资产型号","资产名称",
//                "资产区域","购入日期","使用期限","来源","供应商","使用公司","使用人","存放地方","使用部门","备注"};
//        String[] properties = {"id","changeNo","changeState","changeDate","changeHandler","changeBelongComp","assetIds","assetClassNew","assetModelNew","assetName",
//                "assetRegionNew","assetBuyDateNew","assetLifeNew","assetSourceNew","assetSupplierNew","assetUserCompNew","assetUserNew","assetPlace","assetDept","remarks"};

        String[] header = {"主键ID","处理人","维保信息单号","业务日期","供应商","联系人","联系方式","负责人","维保到期时间","维保说明"};
        String[] properties = {"id","processingPerson","mainInfoNum","workDate","supplier","contacts","contactInfo","personInCharge","expirationTime","explain"};
        MainInfoSingle single = new MainInfoSingle();
        List list = new ArrayList();
        List<MainInfoSingle> mainInfoSingles = mainInfoMapper.selectAllMainInfo(single);
        for (MainInfoSingle mainInfoSingle : mainInfoSingles) {
            list.add(mainInfoSingle);
        }
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelExportHelper.exportExcel(header, properties, list, "维保信息单");
        return workbook;
    }
}
